//
//  HWJingrikaifuViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/8.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWJingrikaifuViewController.h"
#import "HWkaifubiaoCell.h"
#import "HWKfModel.h" // 开服
 

@interface HWJingrikaifuViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;

@end

@implementation HWJingrikaifuViewController


-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {


        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-CF_NavHeight-40) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        


    }
    return   _mainaTableVie ;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    [self addrefreshControl];
    
    
    [self panduanshicunyouci];

}
- (void)panduanshicunyouci {

   if ([self.panduanStr isEqualToString:@"1"]) {
    
            _mainaTableVie.frame = CGRectMake(0, 0, kScreenW, kScreenH-CF_NavHeight-44-40-CF_TabbarHeight);
                            
                            
    
    
   }

}




#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 上拉加载
- (void)underRefresh {
    
   
    
    self.p = @"1";
    
    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"type" : @"0",
                                       @"p" : @"1",
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"kf"
       
                                       };
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
               self.dataMuArr = [HWKfModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               [self.mainaTableVie reloadData];
               
    
           }
        
           
           
       } failure:^(NSString *error) {
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 下拉加载

- (void)onRefresh {
   
  
    NSDictionary *parametes = @{
                                         @"type" : @"0",
                                         @"p" : self.p,
                                         @"ios" : @"1",
                                         @"ct" :  @"app",
                                         @"ac" : @"kf"
         
                                         };
         
         [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
                // self.dataMuArr = [HWKfModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                 
                 int p = [self.p intValue];
                 
                 int max_p = [self.max_p intValue];
                 
                 if (p > max_p) {
                     [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                     
                     return ;
                     
                 }
                 
                
                 NSMutableArray *mutableArr = [HWKfModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                 
        
                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataMuArr addObject:mutableArr[i]];
                     
                 }
                 
                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.mainaTableVie reloadData];
                 
      
             }
          
             
             
         } failure:^(NSString *error) {
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
         }];
    
    
    
    
}












- (void)setupUI{
    self.view.backgroundColor = [UIColor redColor];
    
    UIView  *hederView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 6)];
    hederView.backgroundColor = RGB(239, 236, 239);
    self.mainaTableVie.tableHeaderView = hederView;
    [self.view addSubview:self.mainaTableVie];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.dataMuArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    
    HWkaifubiaoCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"HWkaifubiaoCell" owner:nil options:nil].firstObject;
    }
    cell.model = self.dataMuArr[indexPath.row];
    return  cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return  71;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
     
    
    
     HWKfModel *model =  self.dataMuArr[indexPath.row];
    
    HWGameDetailController *gameDetails = [[HWGameDetailController alloc]init];
    gameDetails.gameId = model.game_id;
    [self.navigationController pushViewController:gameDetails animated:YES];
    
    
    
}







@end
